package Client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.StyledDocument;

import comunicationFrames.Message;

@SuppressWarnings("serial")
public class ConversationFrame extends JFrame {
	
	ComunicatorClient client;
	private long owner;
	private long interlocutorNumber;
	private String interlocutorNick;
	private JPanel contentPane;
	JTextPane textPane;
	StyledDocument doc;
	JTextArea sendingPane;

	public ConversationFrame(ComunicatorClient client,
			long interlocutorNumber) {
		this.client = client;
		this.interlocutorNumber = interlocutorNumber;
		this.interlocutorNick =
				client.getContactsManager().getName(interlocutorNumber);
		this.owner = client.getNumber();
		
		setTitle("Rozmowa z " + interlocutorNick);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 320, 350);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		doc = textPane.getStyledDocument();
		DefaultCaret caret = (DefaultCaret)textPane.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		JScrollPane scrollTextPane = new JScrollPane(textPane);
		contentPane.add(scrollTextPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(null);
		panel.add(panel_1, BorderLayout.WEST);
		
		JButton btnNewButton = new JButton("Wy\u015Blij");
		SendListener sendAction = new SendListener();
		btnNewButton.addActionListener(sendAction);
		panel_1.add(btnNewButton);
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, BorderLayout.CENTER);
		
		sendingPane = new JTextArea();		
		scrollPane.setViewportView(sendingPane);
		
		// Map keys to send a message with enter
		InputMap imap = sendingPane.getInputMap();
		imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK), "panel.newline");
		imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "panel.send");
		imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), "panel.dispose");
		ActionMap amap = sendingPane.getActionMap();
		amap.put("panel.send", sendAction);
		amap.put("panel.newline", new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				sendingPane.setText(sendingPane.getText() + "\n");
			}
		});
		amap.put("panel.dispose", new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				ConversationFrame.this.setVisible(false);
			}
		});
		
		addWindowFocusListener(new WindowAdapter() {
		    public void windowGainedFocus(WindowEvent e) {
		        sendingPane.requestFocusInWindow();
		    }
		});
		
	}
	
	public void displayMessage(Message m) {
		try {
			String message = unpackMessage(m);
			doc.insertString(doc.getLength(), message + "\n", null);
		} catch (BadLocationException e) {
			System.err.println("ConverstaionFrame:" +
					"		    error displaying message: " + e);
		}
	}
	
	public void displayMessage(String m) {
		try {
			doc.insertString(doc.getLength(), m + "\n", null);
		} catch (BadLocationException e) {
			System.err.println("ConverstaionFrame:" +
					"		    error displaying message: " + e);
		}
	}
	
	public long getInterlocutorNumber() {
		return interlocutorNumber;
	}
	
	private String unpackMessage(Message m) {
		return m.getTimestamp() + " " + interlocutorNick + ": "
				+ m.getMessage();
	}
	
	private class SendListener extends AbstractAction {
		@Override
		public void actionPerformed(ActionEvent e) {
			
			Message toSend = new Message(owner, interlocutorNumber,
								    sendingPane.getText());
			String ownerNick = client.getName();
			String toShow = toSend.getTimestamp() + " " + ownerNick + ": "
						    + toSend.getMessage();
			displayMessage(toShow);
			
			//Sending message to addressee
			client.sendMessage(toSend);
			sendingPane.setText("");
		}
		
	}
}
